package DAL;

import Entity.Manager;
import Entity.Operator;
import IDAL.ManagerPort;
import Utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.util.List;

/**
 * @Author Chen
 * @Date 2022/1/7 15:39
 * @Version 1.0
 */
public class ManagerDao implements ManagerPort {
    @Override
    public int addManager(Object... args) {
        int result = 0;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "insert into l_manager(accountNumber,password,name)values(?,?,?)";

            result = runner.update(conn, sql, args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public int updateManager(Object... args) {
        int result = 0;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "update l_manager set " +
                    " password = ?" +
                    " where accountNumber = ?";
            result = runner.update(conn, sql, args);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public int deleteManager(Object... args) {
        int result = 0;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "delete from l_manager where accountNumber=?";
            result = runner.update(conn, sql, args);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public List<Manager> selectManager() {
        List<Manager> result = null;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "select * from l_manager";
            BeanListHandler<Manager> handler = new BeanListHandler<>(Manager.class);
            result = runner.query(conn, sql, handler);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public Manager selectAdminByName(Object... args) {
        Manager result = null;
        Connection conn =null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "select * from l_manager where name = ?";
            BeanHandler<Manager> handler = new BeanHandler<>(Manager.class);
            result = runner.query(conn, sql, handler,args);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn,null);
        }
        return result;
    }

    @Override
    public Manager selectBYManager(Object... args) {
        Manager result = null;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "select * from l_manager where accountNumber = ?";
            BeanHandler<Manager> handler = new BeanHandler<>(Manager.class);
            result = runner.query(conn, sql, handler, args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return  result;
    }
    public  void showAllAdminByName(){
        List<Manager> managers = selectManager();
        if (managers!=null){
            System.out.println("管理员列表如下:");
            System.out.println("管理员Id");
            for (int i=0;i<managers.size();i++){
                System.out.println(managers.get(i));
            }
        }else{
            System.out.println("暂无数据！");
        }
    }

    public static void main(String[] args) {
       ManagerDao managerDao = new ManagerDao();
       managerDao.showAllAdminByName();
    }
}
